package com.bootdo.crm.controller;

import java.util.List;
import java.util.Map;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.crm.domain.ReceivablesDO;
import com.bootdo.crm.service.ReceivablesService;


/**
 * 回款表
 * 
 * @author cui
 * @email 1992lcg@163.com
 * @date 2020-02-20 13:14:28
 */
 
@Controller
@RequestMapping("/crm/receivables")
public class ReceivablesController {
	@Autowired
	private ReceivablesService receivablesService;
	
	@GetMapping()
	@RequiresPermissions("crm:receivables:receivables")
	String Receivables(){
	    return "crm/receivables/receivables";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("crm:receivables:receivables")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ReceivablesDO> receivablesList = receivablesService.list(query);
		int total = receivablesService.count(query);
		PageUtils pageUtils = new PageUtils(receivablesList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("crm:receivables:add")
	String add(){
	    return "crm/receivables/add";
	}

	@GetMapping("/edit/{receivablesId}")
	@RequiresPermissions("crm:receivables:edit")
	String edit(@PathVariable("receivablesId") Integer receivablesId,Model model){
		ReceivablesDO receivables = receivablesService.get(receivablesId);
		model.addAttribute("receivables", receivables);
	    return "crm/receivables/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("crm:receivables:add")
	public R save(ReceivablesDO receivables){
		if(receivablesService.save(receivables)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("crm:receivables:edit")
	public R update( ReceivablesDO receivables){
		receivablesService.update(receivables);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("crm:receivables:remove")
	public R remove( Integer receivablesId){
		if(receivablesService.remove(receivablesId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("crm:receivables:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] receivablesIds){
		receivablesService.batchRemove(receivablesIds);
		return R.ok();
	}
	
}
